package com.examlpe.lotterysystem.controller;

import com.examlpe.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import com.examlpe.lotterysystem.common.exception.ControllerException;
import com.examlpe.lotterysystem.common.pojo.CommonResult;
import com.examlpe.lotterysystem.common.utils.JacksonUtil;
import com.examlpe.lotterysystem.controller.param.CreateActivityParam;
import com.examlpe.lotterysystem.controller.param.CreateUserByActivityParam;
import com.examlpe.lotterysystem.controller.param.PageParam;
import com.examlpe.lotterysystem.controller.result.CreateActivityResult;
import com.examlpe.lotterysystem.controller.result.FindActivityListResult;
import com.examlpe.lotterysystem.controller.result.GetActivityDetailResult;
import com.examlpe.lotterysystem.service.ActivityService;
import com.examlpe.lotterysystem.service.dto.ActivityDTO;
import com.examlpe.lotterysystem.service.dto.ActivityDetailDTO;
import com.examlpe.lotterysystem.service.dto.CreatetivityDTO;
import com.examlpe.lotterysystem.service.dto.PageListDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
public class ActityController {

    @Autowired
    private ActivityService activityService;

    /*
    * 创建活动
    * */
    //userId : 49 郭靖 47 张三
    //prizeId : 20 吹风机 21 手机
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActity(
            @Validated @RequestBody CreateActivityParam param) {
        log.info("createActity CreateActivityParam{}", JacksonUtil.writeValueAsString(param));
       return CommonResult.success(covertToCreateActivityResult(activityService.createActivity(param)));
    }

    @RequestMapping("/activity/find-list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param) {
        log.info("findActivityList PageParam:{}",param);

        return CommonResult.success(
                covertToFindActivityResult(
                        activityService.findActivityList(param)));
    }
    @RequestMapping("/activity-detail/find")
    public CommonResult<GetActivityDetailResult> getActivityDetail(Long activityId) {
        GetActivityDetailResult result =covertToGetActivityDetailResult(activityService.getActivityDetail(activityId));
        return CommonResult.success(result);
    }

    private GetActivityDetailResult covertToGetActivityDetailResult(ActivityDetailDTO activityDetail) {
        if(null == activityDetail) {
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult result = new GetActivityDetailResult();;
        result.setActivityId(activityDetail.getActivityId());
        result.setActivityName(activityDetail.getActivityName());
        result.setDescription(activityDetail.getDesc());
        result.setValid(activityDetail.valid());
        result.setPrizes(activityDetail
                .getPrizeDTOList()
                .stream()
                .sorted(Comparator.comparingInt(prizeDTO -> prizeDTO.getTiers().getCode())) //根据奖品等级排序
                .map(prizeDTO -> {
                    GetActivityDetailResult.Prize prize = new GetActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setName(prizeDTO.getName());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setDescription(prizeDTO.getDescription());
                    prize.setImageUrl(prizeDTO.getImageUrl());
                    prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
                    prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prize.setValid(prizeDTO.valid());
                    return prize;
                }).collect(Collectors.toList()));
        result.setUsers(activityDetail
                .getUserDTOList()
                .stream()
                .map(userDTO -> {
                    GetActivityDetailResult.User user = new GetActivityDetailResult.User();
                    user.setUserId(userDTO.getUserId());
                    user.setUserName(userDTO.getUserName());
                    user.setValid(userDTO.valid());
                    return user;
                }).collect(Collectors.toList()));
        return result;
    }

    private FindActivityListResult covertToFindActivityResult(PageListDTO<ActivityDTO> activityListDTO) {
        if(null == activityListDTO) {
            throw new ControllerException(ControllerErrorCodeConstants.FIND_ACTIVITY_LIST_ERROR);
        }
        FindActivityListResult result = new FindActivityListResult();
        result.setTotal(activityListDTO.getTotal());

        result.setRecords(activityListDTO
                .getRecords()
                .stream()
                .map(activityDTO -> {
                    FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                    activityInfo.setActivityId(activityDTO.getActivityId());
                    activityInfo.setActivityName(activityDTO.getActivityName());
                    activityInfo.setDescription(activityDTO.getDescription());
                    activityInfo.setValid(activityDTO.valid());
                    return activityInfo;
                }).collect(Collectors.toList()));
        return result;
    }

    private CreateActivityResult covertToCreateActivityResult(CreatetivityDTO activity) {
        if(null == activity) {
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result = new CreateActivityResult();
        result.setActivityId(activity.getActivityId());
        return result;
    }

}
